import Cookies from '@/utils/cookies'

const app = {
  state: {
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
      withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'zh',
    size: Cookies.get('size') || 'medium'
  },
  mutations: {
    toggleSideBar: state => {
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
    },
    closeSideBar: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 0)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    toggleDevice: (state, device) => {
      state.device = device
    },
    setLanguage: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    setSize: (state, size) => {
      state.size = size
      Cookies.set('size', size)
    }
  },
  actions: {
    toggleSideBar ({commit}) {
      commit('toggleSideBar')
    },
    closeSideBar ({commit}, {withoutAnimation}) {
      commit('closeSideBar', withoutAnimation)
    },
    toggleDevice ({commit}, device) {
      commit('toggleDevice', device)
    },
    setLanguage ({commit}, language) {
      commit('setLanguage', language)
    },
    setSize ({commit}, size) {
      commit('setSize', size)
    }
  }
}

export default app
